package sns.action;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.Relation;
import sns.bean.User;
import sns.dao.RelationDao;
import sns.dao.UserDao;
import sns.service.RelationService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FollowAction extends ActionSupport {

	// injection
	private RelationService relationService;
	private RelationDao relationDao;
	private UserDao userDao;
	// parameter
	private Relation relation;
	private User currentUser;
	private User visitingUser;
	// value pass in
	private Long currentUserId;

	private Long visitingUserId;
	// return value
	private Long visitId;

	@Transactional
	public String execute() {

		// check if already followed
		currentUser = userDao.findUserById(currentUserId);
		System.out.println("############################## currentUserId"
				+ currentUserId);
		System.out.println("############################## visitingUserId"
				+ visitingUserId);
		visitingUser = userDao.findUserById(visitingUserId);
		visitId = visitingUser.getIduser();
		if (relationService.checkIfFollow(currentUser, visitingUser)) {
			return "ALREADY_FOLLOWED";
		} else {
			relation = new Relation();
			relation.setSource(currentUser);
			relation.setTarget(visitingUser);
			relation.setFollowtime(new Date());
			relationDao.save(relation);
		}
		return Action.SUCCESS;
	}

	public Long getCurrentUserId() {
		return currentUserId;
	}

	public Long getVisitingUserId() {
		return visitingUserId;
	}

	public Long getVisitId() {
		return visitId;
	}

	public void setRelationService(RelationService relationService) {
		this.relationService = relationService;
	}

	public void setRelationDao(RelationDao relationDao) {
		this.relationDao = relationDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setCurrentUserId(Long currentUserId) {
		this.currentUserId = currentUserId;
	}

	public void setVisitingUserId(Long visitingUserId) {
		this.visitingUserId = visitingUserId;
	}

}
